package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.util.Operation;
import com.syxp.pda.core.util.TimeFormatUtil;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.ClassDuty;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.entity.base.User;
import com.syxp.pda.service.base.ClassDutyService;

public class ClassDutyAction extends BaseAction {
	@Resource
	private ClassDutyService classDutyService;

	public ClassDutyService getClassDutyService() {
		return classDutyService;
	}

	public void setClassDutyService(ClassDutyService classDutyService) {
		this.classDutyService = classDutyService;
	}
	private int classID ;
	private int start;
	private int limit;
	
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}
	private String claName ;
	
	private String startTime ;
	
	private String endTime ;
	
	private int departmentID ;

	private String remarks ;
	
	public int getClassID() {
		return classID;
	}

	public void setClassID(int classID) {
		this.classID = classID;
	}

	public String getRemarks() {
		return remarks;
	}

	public void setRemarks(String remarks) {
		this.remarks = remarks;
	}

	public String getClaName() {
		return claName;
	}

	public void setClaName(String claName) {
		this.claName = claName;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public int getDepartmentID() {
		return departmentID;
	}

	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}

	public String load(){
		List<ClassDuty> all = null ;
		QueryFilter filter = new QueryFilter() ;
		if(departmentID==0){
			if(limit==0){
				all = classDutyService.getDao().getAll();
			}else{
				filter.setPagingBean(new PagingBean(start,limit)) ;
				all = classDutyService.getDao().getAll(filter);
			}
			
		}else{
			filter.setPagingBean(new PagingBean(start,limit)) ;
			filter.addFilter(new FieldBean("departmentID" ,departmentID ,Operation.EQUAL));
			all = classDutyService.getDao().getAll(filter);
		}
		JsonArray jarray = new JsonArray() ;
		JsonObject result = new JsonObject() ;
		for(ClassDuty classDuty : all) {
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("name",classDuty.getClaName() ) ;
			jobj.addProperty("startTime", TimeFormatUtil.formatWorkTime(classDuty.getStartTime())) ;
			jobj.addProperty("endTime", TimeFormatUtil.formatWorkTime(classDuty.getEndTime())) ;
			jobj.addProperty("remarks", classDuty.getRemarks()) ;
			jobj.addProperty("id", classDuty.getClassID()) ;
			jarray.add(jobj) ;
		}
		if(limit==0){
			this.jsonString = gson.toJson(jarray) ;
		}else{
			result.addProperty("totalProperty",classDutyService.getDao().getTotalProperty(filter)) ;
			result.add("result", jarray) ;
			result.addProperty("start", start);
			result.addProperty("limit", limit) ;
			this.jsonString="{success:true}" ;
			this.jsonString = gson.toJson(result) ;
		}
		
		System.out.println(jsonString) ;
		return SUCCESS;
	}
	
	public String save() {
		try{
			ClassDuty classDuty = new ClassDuty() ;
			classDuty.setClaName(claName) ;
			classDuty.setStartTime(TimeFormatUtil.parseTime(startTime)) ;
			classDuty.setEndTime(TimeFormatUtil.parseTime(endTime)) ;
			classDuty.setRemarks(remarks) ;
			classDuty.setDepartmentID(departmentID) ;
			classDutyService.addService(classDuty) ;
			JsonObject jobj = new JsonObject() ;
			jobj.addProperty("success", true) ;
			this.jsonString=super.gson.toJson(jobj) ;
		}catch(Exception e) {
			e.printStackTrace();
			this.jsonString="{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String update() {
		try{
			ClassDuty classDuty = new ClassDuty() ;
			classDuty.setClaName(claName) ;
			classDuty.setStartTime(TimeFormatUtil.parseTime(startTime)) ;
			classDuty.setEndTime(TimeFormatUtil.parseTime(endTime)) ;
			classDuty.setRemarks(remarks) ;
			classDutyService.updateService(classDuty) ;
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString = "{success:false}" ;
		}
		return SUCCESS ;
	}
	
	public String delete(){
		try{
			classDutyService.removeService(classID) ;
			this.jsonString = "{success:true}" ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString = "{success:false}" ;
		}
		return SUCCESS ;
	}
}
